import 'package:flutter/material.dart';
import 'package:netflix_social/constants/app_constants.dart';
import 'package:netflix_social/providers/auth_provider.dart';
import 'package:netflix_social/providers/video_provider.dart';
import 'package:netflix_social/providers/social_provider.dart';
import 'package:netflix_social/theme/app_theme.dart';
import 'package:provider/provider.dart';

class ProfileTab extends StatelessWidget {
  const ProfileTab({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final authProvider = Provider.of<AuthProvider>(context);
    final user = authProvider.currentUser;

    return Scaffold(
      backgroundColor: AppTheme.backgroundColor,
      body: SafeArea(
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 用户头部信息
              _buildUserHeader(context, user),
              
              // 会员信息卡片
              _buildMembershipCard(context, user),
              
              // 选项列表
              const SizedBox(height: 24),
              _buildSectionHeader(context, '我的活动'),
              _buildOptionItem(
                context,
                icon: Icons.history,
                title: '观看历史',
                onTap: () {
                  // 导航到观看历史页面
                },
              ),
              _buildOptionItem(
                context,
                icon: Icons.favorite,
                title: '我的收藏',
                onTap: () {
                  // 导航到收藏页面
                },
              ),
              _buildOptionItem(
                context,
                icon: Icons.star,
                title: '我的评分',
                onTap: () {
                  // 导航到评分页面
                },
              ),
              _buildDivider(),
              
              _buildSectionHeader(context, '账户与设置'),
              _buildOptionItem(
                context,
                icon: Icons.person,
                title: '个人资料',
                onTap: () {
                  // 导航到个人资料编辑页面
                },
              ),
              _buildOptionItem(
                context,
                icon: Icons.settings,
                title: '设置',
                onTap: () {
                  // 导航到设置页面
                },
              ),
              _buildOptionItem(
                context,
                icon: Icons.help,
                title: '帮助与反馈',
                onTap: () {
                  // 导航到帮助页面
                },
              ),
              _buildOptionItem(
                context,
                icon: Icons.info,
                title: '关于',
                onTap: () {
                  // 导航到关于页面
                },
              ),
              _buildDivider(),
              
              // 登出按钮
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 24),
                child: ElevatedButton(
                  onPressed: () async {
                    final confirm = await showDialog<bool>(
                      context: context,
                      builder: (context) {
                        return AlertDialog(
                          backgroundColor: AppTheme.cardColor,
                          title: const Text('退出登录'),
                          content: const Text('确定要退出当前账号吗？'),
                          actions: [
                            TextButton(
                              onPressed: () {
                                Navigator.pop(context, false);
                              },
                              child: const Text('取消'),
                            ),
                            TextButton(
                              onPressed: () {
                                Navigator.pop(context, true);
                              },
                              child: const Text(
                                '确定',
                                style: TextStyle(color: AppTheme.netflixRed),
                              ),
                            ),
                          ],
                        );
                      },
                    );
                    
                    if (confirm == true) {
                      await authProvider.logout();
                      if (context.mounted) {
                        Navigator.pushReplacementNamed(context, AppConstants.loginRoute);
                      }
                    }
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.transparent,
                    foregroundColor: Colors.white,
                    side: const BorderSide(color: Colors.grey),
                    padding: const EdgeInsets.symmetric(vertical: 16),
                    minimumSize: const Size(double.infinity, 48),
                  ),
                  child: const Text('退出登录'),
                ),
              ),
              
              // 底部版本信息
              Center(
                child: Padding(
                  padding: const EdgeInsets.only(bottom: 16),
                  child: Text(
                    'v1.0.0',
                    style: TextStyle(
                      color: Colors.grey[600],
                      fontSize: 12,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildUserHeader(BuildContext context, user) {
    return Padding(
      padding: const EdgeInsets.all(16),
      child: Row(
        children: [
          // 用户头像
          CircleAvatar(
            backgroundImage: NetworkImage(user.avatar),
            radius: 40,
          ),
          const SizedBox(width: 16),
          // 用户信息
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  user.username,
                  style: Theme.of(context).textTheme.titleLarge,
                ),
                const SizedBox(height: 4),
                Text(
                  user.email,
                  style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                    color: Colors.grey[400],
                  ),
                ),
                const SizedBox(height: 8),
                Row(
                  children: [
                    _buildProfileCount(
                      context: context,
                      label: '关注',
                      count: user.followingCount,
                    ),
                    Container(
                      height: 20,
                      width: 1,
                      color: Colors.grey[700],
                      margin: const EdgeInsets.symmetric(horizontal: 16),
                    ),
                    _buildProfileCount(
                      context: context,
                      label: '粉丝',
                      count: user.followerCount,
                    ),
                  ],
                ),
              ],
            ),
          ),
          // 编辑按钮
          IconButton(
            icon: const Icon(Icons.edit),
            onPressed: () {
              // 导航到个人资料编辑页面
            },
          ),
        ],
      ),
    );
  }

  Widget _buildProfileCount({
    required BuildContext context,
    required String label,
    required int count,
  }) {
    return InkWell(
      onTap: () {
        // 导航到对应的关注/粉丝列表页面
      },
      child: Row(
        children: [
          Text(
            '$count',
            style: Theme.of(context).textTheme.bodyMedium?.copyWith(
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(width: 4),
          Text(
            label,
            style: Theme.of(context).textTheme.bodyMedium?.copyWith(
              color: Colors.grey[400],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildMembershipCard(BuildContext context, user) {
    final isVip = user.membershipType.toLowerCase() != 'free';
    
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 16),
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: isVip
              ? [
                  Colors.amber.shade700,
                  Colors.amber.shade300,
                ]
              : [
                  Colors.grey.shade800,
                  Colors.grey.shade700,
                ],
        ),
        borderRadius: BorderRadius.circular(12),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                '会员等级',
                style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                  color: isVip ? Colors.black87 : Colors.white70,
                ),
              ),
              if (isVip)
                const Icon(
                  Icons.verified,
                  color: Colors.black87,
                ),
            ],
          ),
          const SizedBox(height: 8),
          Text(
            isVip ? 'VIP 会员' : '免费会员',
            style: Theme.of(context).textTheme.headlineMedium?.copyWith(
              color: isVip ? Colors.black87 : Colors.white,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 12),
          Text(
            isVip
                ? '有效期至 ${user.membershipExpiry}'
                : '升级会员解锁更多精彩内容',
            style: Theme.of(context).textTheme.bodyMedium?.copyWith(
              color: isVip ? Colors.black87 : Colors.white70,
            ),
          ),
          const SizedBox(height: 12),
          ElevatedButton(
            onPressed: () {
              // 导航到会员中心
            },
            style: ElevatedButton.styleFrom(
              backgroundColor: isVip ? Colors.black.withOpacity(0.2) : AppTheme.netflixRed,
              foregroundColor: isVip ? Colors.black87 : Colors.white,
              minimumSize: const Size(double.infinity, 40),
            ),
            child: Text(isVip ? '会员中心' : '立即升级'),
          ),
        ],
      ),
    );
  }

  Widget _buildSectionHeader(BuildContext context, String title) {
    return Padding(
      padding: const EdgeInsets.only(left: 16, top: 8, bottom: 8),
      child: Text(
        title,
        style: Theme.of(context).textTheme.titleMedium,
      ),
    );
  }

  Widget _buildOptionItem(
    BuildContext context, {
    required IconData icon,
    required String title,
    String? subtitle,
    required VoidCallback onTap,
    Widget? trailing,
  }) {
    return InkWell(
      onTap: onTap,
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
        child: Row(
          children: [
            Icon(
              icon,
              color: Colors.grey[400],
            ),
            const SizedBox(width: 16),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    title,
                    style: Theme.of(context).textTheme.bodyLarge,
                  ),
                  if (subtitle != null) ...[
                    const SizedBox(height: 4),
                    Text(
                      subtitle,
                      style: Theme.of(context).textTheme.bodySmall?.copyWith(
                        color: Colors.grey[500],
                      ),
                    ),
                  ],
                ],
              ),
            ),
            trailing ??
                const Icon(
                  Icons.chevron_right,
                  color: Colors.grey,
                ),
          ],
        ),
      ),
    );
  }

  Widget _buildDivider() {
    return Divider(
      height: 1,
      thickness: 1,
      color: Colors.grey[900],
      indent: 16,
      endIndent: 16,
    );
  }
}
